# Copyright 2021 Cortex Labs, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: v1
kind: ConfigMap
metadata:
  name: grafana-datasources
  namespace: default
data:
  datasources.yaml: |
    {
        "apiVersion": 1,
        "datasources": [
            {
                "access": "proxy",
                "editable": false,
                "name": "prometheus",
                "orgId": 1,
                "type": "prometheus",
                "url": "http://prometheus.prometheus:9090",
                "version": 1,
                "isDefault": true
            }
        ]
    }

---

apiVersion: v1
kind: ConfigMap
metadata:
  name: grafana-dashboards
  namespace: default
data:
  dashboards.yaml: |-
    {
        "apiVersion": 1,
        "providers": [
            {
                "folder": "Cortex",
                "name": "Cortex",
                "options": {
                    "path": "/grafana-dashboard-definitions/cortex"
                },
                "disableDeletion": true,
                "orgId": 1,
                "type": "file"
            }
        ]
    }

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: grafana-storage
  namespace: default
spec:
  storageClassName: ssd
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi

---

apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    app: grafana
  name: grafana
  namespace: default
spec:
  serviceName: grafana
  replicas: 1
  selector:
    matchLabels:
      app: grafana
  template:
    metadata:
      labels:
        app: grafana
    spec:
      containers:
        - image: {{ config['image_grafana'] }}
          name: grafana
          ports:
            - containerPort: 3000
              name: http
          readinessProbe:
            httpGet:
              path: /api/health
              port: http
          resources:
            limits:
              cpu: 200m
              memory: 200Mi
            requests:
              cpu: 100m
              memory: 100Mi
          env:
            - name: GF_SERVER_ROOT_URL
              value: "%(protocol)s://%(domain)s:%(http_port)s/dashboard"
            - name: GF_SERVER_SERVE_FROM_SUB_PATH
              value: "true"
            - name: GF_USERS_DEFAULT_THEME
              value: "light"
          volumeMounts:
            - mountPath: /var/lib/grafana
              name: grafana-storage
              readOnly: false
            - mountPath: /etc/grafana/provisioning/datasources
              name: grafana-datasources
              readOnly: false
            - mountPath: /etc/grafana/provisioning/dashboards
              name: grafana-dashboards
              readOnly: false
            - mountPath: /grafana-dashboard-definitions/cortex/realtime
              name: grafana-dashboard-realtime
              readOnly: false
            - mountPath: /grafana-dashboard-definitions/cortex/async
              name: grafana-dashboard-async
              readOnly: false
            - mountPath: /grafana-dashboard-definitions/cortex/batch
              name: grafana-dashboard-batch
              readOnly: false
            - mountPath: /grafana-dashboard-definitions/cortex/task
              name: grafana-dashboard-task
              readOnly: false
            - mountPath: /grafana-dashboard-definitions/cortex/cluster
              name: grafana-dashboard-cluster
              readOnly: false
            - mountPath: /grafana-dashboard-definitions/cortex/nodes
              name: grafana-dashboard-nodes
              readOnly: false
            {% if env.get("CORTEX_DEV_ADD_CONTROL_PLANE_DASHBOARD") == "true" %}
            - mountPath: /grafana-dashboard-definitions/cortex/control-plane
              name: grafana-dashboard-control-plane
              readOnly: false
            {% endif %}
      securityContext:
        fsGroup: 65534
        runAsNonRoot: true
        runAsUser: 65534
      volumes:
        - name: grafana-storage
          persistentVolumeClaim:
            claimName: grafana-storage
        - name: grafana-datasources
          configMap:
            name: grafana-datasources
        - name: grafana-dashboards
          configMap:
            name: grafana-dashboards
        - name: grafana-dashboard-realtime
          configMap:
            name: grafana-dashboard-realtime
        - name: grafana-dashboard-async
          configMap:
            name: grafana-dashboard-async
        - name: grafana-dashboard-batch
          configMap:
            name: grafana-dashboard-batch
        - name: grafana-dashboard-task
          configMap:
            name: grafana-dashboard-task
        - name: grafana-dashboard-cluster
          configMap:
            name: grafana-dashboard-cluster
        - name: grafana-dashboard-nodes
          configMap:
            name: grafana-dashboard-nodes
        {% if env.get("CORTEX_DEV_ADD_CONTROL_PLANE_DASHBOARD") == "true" %}
        - name: grafana-dashboard-control-plane
          configMap:
            name: grafana-dashboard-control-plane
        {% endif %}
      nodeSelector:
        prometheus: "true"
      tolerations:
        - key: prometheus
          operator: Exists
          effect: NoSchedule
      affinity:
        podAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - podAffinityTerm:
                labelSelector:
                  matchLabels:
                    prometheus: prometheus
                topologyKey: kubernetes.io/hostname
              weight: 100

---

apiVersion: v1
kind: Service
metadata:
  labels:
    app: grafana
  name: grafana
  namespace: default
spec:
  type: ClusterIP
  ports:
    - name: http
      port: 3000
      targetPort: http
  selector:
    app: grafana

---

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: grafana
  namespace: default
spec:
  hosts:
    - "*"
  gateways:
    - operator-gateway
  http:
    - name: grafana
      match:
        - uri:
            prefix: "/dashboard"
        - uri:
            prefix: "/grafana"
      rewrite:
        uri: "/dashboard"
      route:
        - destination:
            host: grafana
            port:
              number: 3000
